using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EuropeEngulfed.NET
{
    partial class ForcePoolDialog : Form
    {
        private MapInfo.CountryID country;
        private List<PlayingPiece> forcePoolList;
        private PlayingPiece selectedPlayingPiece = null;
        private bool buildUnitDialog = true;
        private bool buildAtFullStrength = false;
        public delegate bool CanBlockBeBuiltAtFullStrength(Block block);
        private CanBlockBeBuiltAtFullStrength blockTest = null;

        public bool BuildAtFullStrength
        {
            get
            {
                return buildAtFullStrength;
            }
        }

        public PlayingPiece SelectedPiece
        {
            get
            {
                return selectedPlayingPiece;
            }
        }

        public ForcePoolDialog(List<PlayingPiece> forcePoolList, CanBlockBeBuiltAtFullStrength blockTest)
        {
            InitializeComponent();
            this.forcePoolList = forcePoolList;
            if (forcePoolList.Count > 0)
                country = forcePoolList[0].CountryOwner;
            else
            {
                country = MapInfo.CountryID.BalticStates;
            }

            this.forcePoolListView.View = View.Tile;
            this.forcePoolListView.TileSize = new Size(PlayingPieceGraphics.GetBlockSize().Width * 2, PlayingPieceGraphics.GetBlockSize().Height * 2);
            foreach(PlayingPiece playingPiece in forcePoolList)
            {
                this.forcePoolListView.Items.Add(playingPiece.GetDescription());
            }

            this.blockTest = blockTest;
        }

        public ForcePoolDialog(List<PlayingPiece> forcePoolList, bool closeOnDoubleClick) : this(forcePoolList, null)
        {
            this.buildUnitDialog = closeOnDoubleClick;
        }

        private void forcePoolListView_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            e.DrawText(TextFormatFlags.Bottom | TextFormatFlags.HorizontalCenter);
            PlayingPieceGraphics.PaintPlayingPiece(e.Graphics, forcePoolList[e.ItemIndex], new Point(e.Bounds.Left + (e.Bounds.Width / 2), e.Bounds.Top + (e.Bounds.Height / 2)));
            e.DrawFocusRectangle();
        }

        private void ForcePoolDialog_Paint(object sender, PaintEventArgs e)
        {
            if(MapInfo.IsMajorPower(country))
                PlayingPieceGraphics.PaintControlMarker(e.Graphics, country, new Point(155, 23), false);
            else if(MapInfo.IsAxisMinorPower(country))
            {
                PlayingPieceGraphics.PaintControlMarker(e.Graphics, MapInfo.CountryID.Germany, new Point(155, 23), false);
            }
        }

        private void OkButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void forcePoolListView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Left && this.buildUnitDialog)
            {
                selectedPlayingPiece = forcePoolList[this.forcePoolListView.SelectedItems[0].Index];
                this.Close();
            }
        }

        private void ForcePoolDialog_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && this.buildUnitDialog)
            {
                if (forcePoolList[this.forcePoolListView.SelectedItems[0].Index] != null)
                {
                    if (forcePoolList[this.forcePoolListView.SelectedItems[0].Index] is Block)
                    {
                        if (blockTest((Block)forcePoolList[this.forcePoolListView.SelectedItems[0].Index]))
                        {
                            ContextMenu buildContextMenu = new ContextMenu();
                            MenuItem buildAtFullStrength = new MenuItem("Build Full Strength Block");
                            buildAtFullStrength.Click += new EventHandler(buildAtFullStrength_Click);
                            buildContextMenu.MenuItems.Add(buildAtFullStrength);
                            buildContextMenu.Show(this, e.Location);
                        }
                    }
                }
            }
        }

        void buildAtFullStrength_Click(object sender, EventArgs e)
        {
            selectedPlayingPiece = forcePoolList[this.forcePoolListView.SelectedItems[0].Index];
            this.buildAtFullStrength = true;
            this.Close();
        }
    }
}